import type {
  AuxiliaryInputDownloadTemplateParams,
  AuxiliaryInputImportExcelParams,
  AuxiliaryInputInfo,
  AuxiliaryInputQueryListParams,
  AuxiliaryInputRepairRecordParams,
  AuxiliaryInputSaveOrUpdateParams,
} from '@/types'

const controller = getAjaxBaseUrl(
  '/auxiliaryInput',
  '/energy',
)

/** 新增或修改企业产能信息 */
export function saveOrUpdate(params: Partial<AuxiliaryInputSaveOrUpdateParams>) {
  return post(
    `${controller}/saveOrUpdateEnterpriseCapacity`,
    params,
  )
}

/** 查询企业年度产能信息 */
export function queryList(params?: Partial<AuxiliaryInputQueryListParams>) {
  return get<AuxiliaryInputInfo[]>(
    `${controller}/loadYearEnterpriseCapacityList`,
    params,
  )
}

/** 删除企业产能信息 */
export function deleteById(id: number) {
  return get(`${controller}/deleteEnterpriseCapacity`, { id })
}

/** 下载企业资源消耗测点数据模板 */
export async function downloadTemplate(params: AuxiliaryInputDownloadTemplateParams) {
  const { data } = await request({
    method: 'get',
    url: `${controller}/downloadTemplate`,
    responseType: 'blob',
    params,
  })

  downloadByA(
    URL.createObjectURL(data),
    `资源消耗测点数据模板_${formatTime(
      Date.now(),
      'YYYY-MM-DD_hh-mm-ss',
    )}`,
  )
}

/** 批量导入企业资源消耗数据 */
export async function importExcel(params: AuxiliaryInputImportExcelParams) {
  return post(
    `${controller}/batchSaveOrUpdateEnergyConsume`,
    params,
    {
      'Content-Type': 'multipart/form-data',
    },
  )
}

/** 企业资源消耗数据补录 */
export function repairRecord(params: Partial<AuxiliaryInputRepairRecordParams>) {
  return post(`${controller}/recordEnergyConsume`, params)
}

/** 查询最新目标产能 */
export function loadNewTarget() {
  return get<AuxiliaryInputInfo>(`${controller}/loadNewTarget`)
}
